<?php

namespace Common\Model;
use Think\Model;
class CommentModel extends Model{
	public $pagesize = 10;
	
	/**
	 * 提交评论
	 * @param  string  $type                  [description]
	 * @param  string  $user_id               [description]
	 * @param  string  $other_id              [description]
	 * @param  string  $content               [description]
	 * @param  string  $reply_id              [description]
	 * @param  [type]  $agreement_id          [description]
	 * @param  string  $order_user_id         [description]
	 * @param  integer $finish_quality_star   [description]
	 * @param  integer $working_speed_star    [description]
	 * @param  integer $service_attitude_star [description]
	 * @param  string  $label_id              [description]
	 * @param  integer $admin_id              [description]
	 * @return [type]                         [description]
	 */
	public function submit($type = 'article', $user_id = '', $other_id = '',$content = '', $reply_id = '',  $agreement_id, $order_user_id = '', $finish_quality_star = 0, $working_speed_star = 0, $service_attitude_star = 0, $label_id = '', $admin_id = 0){
		$data['type']                  = $type;
		$data['user_id']               = $user_id ? $user_id : 0;
		$data['other_id']              = $other_id ? $other_id : 0;
		$data['content']               = $content;
		$data['reply_id']              = $reply_id ? $reply_id : 0;
		$data['agreement_id']          = $agreement_id ? $agreement_id : 0;
		$data['order_user_id']         = $order_user_id ? $order_user_id : 0;
		$data['finish_quality_star']   = $finish_quality_star ? $finish_quality_star : 0;
		$data['working_speed_star']    = $working_speed_star ? $working_speed_star : 0;
		$data['service_attitude_star'] = $service_attitude_star ? $service_attitude_star : 0;
		$data['label_id']              = $label_id ? $label_id : '';
		$data['admin_id']              = $admin_id ? $admin_id : 0;
		$data['create_time']           = time();
		$data['ip']                    = get_client_ip();
		$data['status']                = 1;

		// 更新平台评价数据
		if($type == 'audit_comment'){
			// 更新订单平均分
			$this->update_order_avg_score('platform', $other_id, $finish_quality_star, $working_speed_star, $service_attitude_star);
			// 更新用户平均分
			$this->update_lawyer_avg_score('platform', $order_user_id, $finish_quality_star, $working_speed_star, $service_attitude_star);
		}
		// 更新用户评价数据
		if($type == 'user_comment'){
			// 更新订单平均分
			$this->update_order_avg_score('user', $other_id, $finish_quality_star, $working_speed_star, $service_attitude_star);
			// 更新用户平均分
			$this->update_lawyer_avg_score('user', $order_user_id, $finish_quality_star, $working_speed_star, $service_attitude_star);
			// 更新订单评价
			$this->update_order_comment($other_id);
		}
		// 更新合同平均分
		if($agreement_id){
			$this->update_agreement_avg_score($agreement_id, $finish_quality_star, $working_speed_star, $service_attitude_star);
		}
		// 更新评价标签
		if($label_id && $agreement_id){
			foreach (explode(',', $label_id) as $lid) {
				$this->update_label_num($agreement_id, $lid);
			}
		}
		$this->add($data);

		return $this->getLastInsID();
	}
	/**
	 * 更新订单评价
	 * @param  [type] $order_sn 订单号
	 */
	public function update_order_comment($order_sn){
		if(!$order_sn){
			return false;
		}
		$data['edit_time']    = time();
		$data['comment_time'] = time();
		$data['is_commented'] = 1;

		$where['order_sn'] = $order_sn;
		D('Order')->where($where)->save($data);

		return true;
	}
	/**
	 * 更新订单评分
	 * @param  string $type     类型 user 更新用户评分，platform更新平台评分
	 * @param  [type] $order_sn 订单号
	 */
	public function update_order_avg_score($type = 'platform', $order_sn, $finish_quality_star = 0, $working_speed_star = 0, $service_attitude_star = 0){
		$total_avg = $finish_quality_star + $working_speed_star + $service_attitude_star;
		$avg = sprintf("%.1f", $total_avg / 3);
		switch ($type) {
			case 'platform':
				$data['platform_avg_score'] = $avg;
				break;

			case 'user':
				$data['user_avg_score'] = $avg;
				break;
			
			default:
				# code...
				break;
		}
		$data['edit_time'] = time();
		$where['order_sn'] = $order_sn;
		D('Order')->where(array('order_sn' => $order_sn))->save($data);
		// 更新总评分
		$this->update_order_total_avg_score($order_sn);
	}
	/**
	 * 更新订单总评分
	 * @param  [type] $order_sn 订单号
	 */
	public function update_order_total_avg_score($order_sn){
		$where['order_sn'] = $order_sn;
		$info = D('Order')->where($where)->field('platform_avg_score,user_avg_score')->find();
		$total_avg = $info['platform_avg_score'] + $info['user_avg_score'];
		$avg = sprintf("%.1f", $total_avg / 2);
		D('Order')->where($where)->save(array('total_avg_score' => $avg));
	}
	/**
	 * 更新律师平均分
	 * @param  string  $type                  [description]
	 * @param  [type]  $user_id               [description]
	 * @param  integer $finish_quality_star   [description]
	 * @param  integer $working_speed_star    [description]
	 * @param  integer $service_attitude_star [description]
	 * @return [type]                         [description]
	 */
	public function update_lawyer_avg_score($type = 'platform', $user_id, $finish_quality_star = 0, $working_speed_star = 0, $service_attitude_star = 0){
		$total_avg = $finish_quality_star + $working_speed_star + $service_attitude_star;
		$avg = $total_avg / 3;
		$where['user_id'] = $user_id;
		$info = D('UserLawyer')->where($where)->find();
		switch ($type) {
			case 'platform':
				$user_avg = $info['platform_avg_score'];
				$data['platform_avg_score'] = sprintf("%.1f", $user_avg > 0 ? ($avg + $user_avg) / 2 : $avg);
				break;

			case 'user':
				$user_avg = $info['user_avg_score'];
				$data['user_avg_score'] = sprintf("%.1f", $user_avg > 0 ? ($avg + $user_avg) / 2 : $avg);
				break;
			
			default:
				# code...
				break;
		}
		$data['finish_quality_avg']   = sprintf("%.1f", $info['finish_quality_avg'] > 0 ? (($finish_quality_star + $info['finish_quality_avg']) / 2) : $finish_quality_star);
		$data['working_speed_avg']    = sprintf("%.1f", $info['working_speed_avg'] > 0 ? (($working_speed_star + $info['working_speed_avg']) / 2) : $working_speed_star);
		$data['service_attitude_avg'] = sprintf("%.1f", $info['service_attitude_avg'] > 0 ? (($service_attitude_star + $info['service_attitude_avg']) / 2) : $service_attitude_star);
		D('UserLawyer')->where($where)->save($data);
		// 更新总评分
		$this->update_lawyer_total_avg_score($user_id);
	}
	/**
	 * 更新律师总平均分
	 * @param  [type] $user_id [description]
	 * @return [type]          [description]
	 */
	public function update_lawyer_total_avg_score($user_id){
		$where['user_id'] = $user_id;
		$info = D('UserLawyer')->where($where)->field('platform_avg_score,user_avg_score')->find();
		$total_avg = $info['platform_avg_score'] + $info['user_avg_score'];
		$avg = sprintf("%.1f", $total_avg / 2);
		D('UserLawyer')->where($where)->save(array('total_avg_score' => $avg));
	}
	/**
	 * 更新合同评分
	 * @param  [type]  $agreement_id          [description]
	 * @param  integer $finish_quality_star   [description]
	 * @param  integer $working_speed_star    [description]
	 * @param  integer $service_attitude_star [description]
	 * @return [type]                         [description]
	 */
	public function update_agreement_avg_score($agreement_id, $finish_quality_star = 0, $working_speed_star = 0, $service_attitude_star = 0){
		$total_avg = $finish_quality_star + $working_speed_star + $service_attitude_star;
		$avg = $total_avg / 3;
		$where['id'] = $agreement_id;
		$info = D('CustomAgreement')->where($where)->find();

		$data['finish_quality_avg']   = sprintf("%.1f", $info['finish_quality_avg'] > 0 ? (($finish_quality_star + $info['finish_quality_avg']) / 2) : $finish_quality_star);
		$data['working_speed_avg']    = sprintf("%.1f", $info['working_speed_avg'] > 0 ? (($working_speed_star + $info['working_speed_avg']) / 2) : $working_speed_star);
		$data['service_attitude_avg'] = sprintf("%.1f", $info['service_attitude_avg'] > 0 ? (($service_attitude_star + $info['service_attitude_avg']) / 2) : $service_attitude_star);
		$data['total_avg']            = sprintf("%.1f", ($data['finish_quality_avg'] + $data['working_speed_avg'] + $data['service_attitude_avg']) / 3);
		$data['comment_num']          = array('exp', 'comment_num + 1');
		D('CustomAgreement')->where($where)->save($data);
	}


	/**
	 * 获取评价列表
	 * @param  array   $where    where条件
	 * @param  string  $order    排序
	 * @param  integer $page     第几页
	 * @param  integer $pagesize 分页数量
	 */
	public function get_list($where, $order = '', $page = 1, $pagesize = 0){
		$pagesize = $pagesize ? $pagesize : $this->pagesize;
		$first_row = $pagesize * ($page - 1);

		//默认排序
		if(!$order){
			$order['create_time'] = 'desc';
			$order['id']          = 'desc';
		}

		$where['status']    = 1;
		$count = $this->where($where)->count();
		$list  = $this->where($where)->limit($first_row . ',' . $pagesize)->order($order)->select();
		foreach ($list as $rs) {
			$new_list[] = $this->filter_field($rs);
		}
		$this->count = $count;
		return $new_list ? $new_list : array();
	}
	public function filter_field($data){
		$data['time_desc'] = time_ago_desc($data['create_time']);
		$data['user_name'] = $data['user_id'] ? D('User')->get_user_name($data['user_id']) : '';
		$data['user_head'] = $data['user_id'] ? D('User')->get_user_head_url($data['user_id']) : '';
		$data['user_mobile'] = $data['user_id'] ? D('User')->get_user_mobile($data['user_id']) : '';
		if(!$data['user_id'] && $data['admin_id']){
			$data['user_name']   = $data['admin_id'] ? D('User')->get_admin_name($data['admin_id']) : '';
			$data['user_mobile'] = $data['admin_id'] ? D('User')->get_admin_mobile($data['admin_id']) : '';
		}
		// 平均分
		$data['total_avg'] = ($data['finish_quality_star'] + $data['working_speed_star'] + $data['service_attitude_star']) / 3;
		$label_list = array();
		if($data['label_id']){
			$where['id'] = array('in', $data['label_id']);
			$label_list = D('Category')->where($where)->select();
		}
		$data['label_name_list'] = $label_list;

		$data['finish_quality_star_desc']   = $this->star_desc($data['finish_quality_star']);
		$data['working_speed_star_desc']    = $this->star_desc($data['working_speed_star']);
		$data['service_attitude_star_desc'] = $this->star_desc($data['service_attitude_star']);

		return $data;
	}
	public function star_desc($star){
		if($star < 3){
			$desc = '差';
		}
		elseif($star >= 3 && $star < 4){
			$desc = '中';
		}else{
			$desc = '好';
		}
		return $desc;
	}
	/**
	 * 获取评论数
	 * @param  [type] $other_id 其它ID，如文章ID
	 * @param  string $type     类型
	 */
	public function get_comment_num($other_id, $type = 'article', $agreement_id = ''){
		if($agreement_id){
			$where['agreement_id'] = $agreement_id;
		}
		$where['type']     = $type;
		$where['other_id'] = $other_id;
		$where['status']   = 1;
		$count = $this->where($where)->count();

		return $count ? $count : 0;
	}
	public function get_order_comment_info($order_sn = '', $type = 'audit_comment'){
		$where['other_id'] = $order_sn;
		$where['type']     = $type;
		$data = $this->where($where)->find();
		$info = $this->filter_field($data);

		return $data ? $info : array();
	}
	/**
	 * 更新标签数量
	 * @param  [type] $agreement_id 合同ID
	 * @param  [type] $label_id     标签ID
	 */
	public function update_label_num($agreement_id, $label_id){
		$where['agreement_id'] = $agreement_id;
		$where['label_id']     = $label_id;
		if(D('CommentLabel')->where($where)->count()){
			D('CommentLabel')->where($where)->setInc('num');
		}else{
			$where['label_name'] = D('Category')->get_name($label_id);
			D('CommentLabel')->add($where);
		}
	}
	/**
	 * 获取评价标签列表
	 * @param  [type] $agreement_id 合同ID
	 */
	public function get_comment_label($agreement_id){
		$where['agreement_id'] = $agreement_id;
		$list = D('CommentLabel')->where($where)->select();
		
		return $list ? $list : array();
	}
}